<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
        
        
        <link rel="shortcut icon" href="../img/favicon.ico">
        <title>web3 API - Web3.js 0.20官方文档中文版</title>
        <link href="../css/bootstrap-custom.min.css" rel="stylesheet">
        <link href="../css/font-awesome.min.css" rel="stylesheet">
        <link href="../css/base.css" rel="stylesheet">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css">
        <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->

        <script src="../js/jquery-1.10.2.min.js" defer></script>
        <script src="../js/bootstrap-3.0.3.min.js" defer></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
        <script>hljs.initHighlightingOnLoad();</script> 
    </head>

    <body>

        <div class="navbar navbar-default navbar-fixed-top" role="navigation">
            <div class="container">

                <!-- Collapsed navigation -->
                <div class="navbar-header">
                    <!-- Expander button -->
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="..">Web3.js 0.20官方文档中文版</a>
                </div>

                <!-- Expanded navigation -->
                <div class="navbar-collapse collapse">
                        <!-- Main navigation -->
                        <ul class="nav navbar-nav">
                            <li >
                                <a href="https://web3.learnblockchain.cn">首页</a>
                            </li>
                            <li >
                                <a href="..">0.20文档</a>
                            </li>
                            <li >
                                <a href="https://learnblockchain.cn">深入浅出区块链</a>
                            </li>
                            <li >
                                <a href="https://wiki.learnblockchain.cn/images/dapp.png">DAPP视频课程</a>
                            </li>
                        </ul>

                    <ul class="nav navbar-nav navbar-right">
                        <li>
                            <a href="#" data-toggle="modal" data-target="#mkdocs_search_modal">
                                <i class="fa fa-search"></i> Search
                            </a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="container">
                <div class="col-md-3"><div class="bs-sidebar hidden-print affix well" role="complementary">
    <ul class="nav bs-sidenav">
        <li class="main active"><a href="#web3-api">web3 API</a></li>
            <li><a href="#web3">web3</a></li>
            <li><a href="#web3version">web3.version</a></li>
            <li><a href="#web3isconnected">web3.isConnected</a></li>
            <li><a href="#web3setprovider">web3.setProvider</a></li>
            <li><a href="#web3currentprovider">web3.currentProvider</a></li>
            <li><a href="#web3reset">web3.reset</a></li>
            <li><a href="#web3sha3">web3.sha3</a></li>
            <li><a href="#web3tohex">web3.toHex</a></li>
            <li><a href="#web3toascii">web3.toAscii</a></li>
            <li><a href="#web3fromascii">web3.fromAscii</a></li>
            <li><a href="#web3todecimal">web3.toDecimal</a></li>
            <li><a href="#web3fromdecimal">web3.fromDecimal</a></li>
            <li><a href="#web3fromwei">web3.fromWei</a></li>
            <li><a href="#web3towei">web3.toWei</a></li>
            <li><a href="#web3tobignumber">web3.toBigNumber</a></li>
            <li><a href="#web3isaddress">web3.isAddress</a></li>
    </ul>
</div></div>
                <div class="col-md-9" role="main">

<h1 id="web3-api">web3 API</h1>
<h2 id="web3">web3</h2>
<p><code>web3</code> 对象提供了所有方法</p>
<h3 id="_1">示例</h3>
<pre><code class="js">var Web3 = require('web3');
// create an instance of web3 using the HTTP provider.
// NOTE in mist web3 is already available, so check first if it's available before instantiating
var web3 = new Web3(new Web3.providers.HttpProvider(&quot;http://localhost:8545&quot;));
</code></pre>

<h4 id="using-http-basic-authentication">示例 using HTTP Basic Authentication</h4>
<pre><code class="js">var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider(&quot;http://localhost:8545&quot;, 0, BasicAuthUsername, BasicAuthPassword));
//Note: HttpProvider takes 4 arguments (host, timeout, user, password)
</code></pre>

<h2 id="web3version">web3.version</h2>
<h3 id="web3versionapi">web3.version.api</h3>
<pre><code class="js">web3.version.api
</code></pre>

<h3 id="_2">返回值</h3>
<p><code>String</code> - 以太坊js的api版本.</p>
<h3 id="_3">示例</h3>
<pre><code class="js">var version = web3.version.api;
console.log(version); // &quot;0.2.0&quot;
</code></pre>

<hr />
<h3 id="web3versionnode">web3.version.node</h3>
<pre><code>web3.version.node
// or async
web3.version.getNode(callback(error, result){ ... })
</code></pre>
<h4 id="_4">返回值</h4>
<p><code>String</code> - 客户端或节点的版本信息.</p>
<h4 id="_5">示例</h4>
<pre><code class="js">var version = web3.version.node;
console.log(version); // &quot;Mist/v0.9.3/darwin/go1.4.1&quot;
</code></pre>

<hr />
<h3 id="web3versionnetwork">web3.version.network</h3>
<pre><code>web3.version.network
// or async
web3.version.getNetwork(callback(error, result){ ... })
</code></pre>
<h4 id="_6">返回值</h4>
<p><code>String</code> - 网络协议版本.</p>
<h4 id="_7">示例</h4>
<pre><code class="js">var version = web3.version.network;
console.log(version); // 54
</code></pre>

<hr />
<h3 id="web3versionethereum">web3.version.ethereum</h3>
<pre><code>web3.version.ethereum
// or async
web3.version.getEthereum(callback(error, result){ ... })
</code></pre>
<h4 id="_8">返回值</h4>
<p><code>String</code> - 以太坊的协议版本.</p>
<h4 id="_9">示例</h4>
<pre><code class="js">var version = web3.version.ethereum;
console.log(version); // 60
</code></pre>

<hr />
<h3 id="web3versionwhisper">web3.version.whisper</h3>
<pre><code>web3.version.whisper
// or async
web3.version.getWhisper(callback(error, result){ ... })
</code></pre>
<h4 id="_10">返回值</h4>
<p><code>String</code> - Twhisper协议的版本.</p>
<h4 id="_11">示例</h4>
<pre><code class="js">var version = web3.version.whisper;
console.log(version); // 20
</code></pre>

<hr />
<h2 id="web3isconnected">web3.isConnected</h2>
<pre><code>web3.isConnected()
</code></pre>
<p>检查是否与节点连接上</p>
<h3 id="_12">参数</h3>
<p>无</p>
<h3 id="_13">返回值</h3>
<p><code>Boolean</code>:true表示已连接</p>
<h3 id="_14">示例</h3>
<pre><code class="js">if(!web3.isConnected()) {

   // show some dialog to ask the user to start a node

} else {

   // start web3 filters, calls, etc

}
</code></pre>

<hr />
<h2 id="web3setprovider">web3.setProvider</h2>
<pre><code>web3.setProvider(provider)
</code></pre>
<p>设置网络通信服务提供对象.</p>
<h3 id="_15">参数</h3>
<p>none</p>
<h3 id="_16">返回值</h3>
<p><code>undefined</code>:通信服务提供对象</p>
<h3 id="_17">示例</h3>
<pre><code class="js">web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545')); // 8080 for cpp/AZ, 8545 for go/mist
</code></pre>

<hr />
<h2 id="web3currentprovider">web3.currentProvider</h2>
<pre><code>web3.currentProvider
</code></pre>
<p>如果已经设置了Provider，则返回当前的Provider。这个方法可以用来检查在使用mist浏览器等情况下已经设置过Provider，避免重复设置的情况.</p>
<h3 id="_18">返回值</h3>
<p><code>Object</code> - null 或 已经设置的Provider对象;</p>
<h3 id="_19">示例</h3>
<pre><code class="js">// Check if mist etc. already set a provider
if(!web3.currentProvider)
    web3.setProvider(new web3.providers.HttpProvider(&quot;http://localhost:8545&quot;));

</code></pre>

<hr />
<h2 id="web3reset">web3.reset</h2>
<pre><code>web3.reset(keepIsSyncing)
</code></pre>
<p>用来重置web3的状态。重置除了manager以外的其它所有东西。卸载filter，停止状态轮询</p>
<h3 id="_20">参数</h3>
<ol>
<li><code>flag``Boolean</code> - 如果设置为true，将会卸载所有的filter，但会保留web3.eth.isSyncing()的状态轮询</li>
</ol>
<h3 id="_21">返回值</h3>
<p><code>undefined</code></p>
<h3 id="_22">示例</h3>
<pre><code class="js">web3.reset();
</code></pre>

<hr />
<h2 id="web3sha3">web3.sha3</h2>
<p>使用keccak-256哈希算法，计算给定字符串的哈希值</p>
<pre><code>web3.sha3(string [, options])
</code></pre>
<h3 id="_23">参数</h3>
<ol>
<li><code>String</code> - 传入的需要使用Keccak-256 SHA3算法进行哈希运算的字符串.</li>
<li><code>Object</code> - 可选项设置。如果要解析的是hex格式的十六进制字符串。需要设置encoding为hex。因为JS中会默认忽略0x.</li>
</ol>
<h3 id="_24">返回值</h3>
<p><code>String</code> - 使用Keccak-256 SHA3算法哈希过的结果.</p>
<h3 id="_25">示例</h3>
<pre><code class="js">var hash = web3.sha3(&quot;Some string to be hashed&quot;);
console.log(hash); // &quot;0xed973b234cf2238052c9ac87072c71bcf33abc1bbd721018e0cca448ef79b379&quot;

var hashOfHash = web3.sha3(hash, {encoding: 'hex'});
console.log(hashOfHash); // &quot;0x85dd39c91a64167ba20732b228251e67caed1462d4bcf036af88dc6856d0fdcc&quot;
</code></pre>

<hr />
<h2 id="web3tohex">web3.toHex</h2>
<pre><code>web3.toHex(mixed);
</code></pre>
<p>Converts any value into HEX.</p>
<h3 id="_26">参数</h3>
<ol>
<li><code>val``String|Number|Object|Array|BigNumber</code> - 需要转化为HEX的值。如果是一个对象或数组类型，将会先用JSON.stringify1进行转换成字符串。 如果传入的是BigNumber2，则将得到对应的Number的HEX.</li>
</ol>
<h3 id="_27">返回值</h3>
<p><code>String</code> - The hex string of <code>mixed</code>.</p>
<h3 id="_28">示例</h3>
<pre><code class="js">var str = web3.toHex({test: 'test'});
console.log(str); // '0x7b2274657374223a2274657374227d'
</code></pre>

<hr />
<h2 id="web3toascii">web3.toAscii</h2>
<p>将HEX字符串转为ASCII3字符串</p>
<pre><code>web3.toAscii(hexString);
</code></pre>
<h3 id="_29">参数</h3>
<p>1.<code>hexString</code> <code>String</code> - 十六进制字符串.</p>
<h3 id="_30">返回值</h3>
<p><code>String</code> - 给定十六进制字符串对应的ASCII码值.</p>
<h3 id="_31">示例</h3>
<pre><code class="js">var str = web3.toAscii(&quot;0x657468657265756d000000000000000000000000000000000000000000000000&quot;);
console.log(str); // &quot;ethereum&quot;
</code></pre>

<hr />
<h2 id="web3fromascii">web3.fromAscii</h2>
<pre><code>web3.fromAscii(string [, padding]);
</code></pre>
<p>将任何的ASCII码字符串转为HEX字符串.</p>
<h3 id="_32">参数</h3>
<ol>
<li><code>textString``String</code> -ASCII码字符串.</li>
<li><code>padding``Number</code> - 返回的字符串字节大小，不够长会自动填充.</li>
</ol>
<h3 id="_33">返回值</h3>
<p><code>String</code> - 转换后的HEX字符串.</p>
<h3 id="_34">示例</h3>
<pre><code class="js">var str = web3.fromAscii('ethereum');
console.log(str); // &quot;0x657468657265756d&quot;

var str2 = web3.fromAscii('ethereum', 32);
console.log(str2); // &quot;0x657468657265756d000000000000000000000000000000000000000000000000&quot;
</code></pre>

<hr />
<h2 id="web3todecimal">web3.toDecimal</h2>
<pre><code>web3.toDecimal(hexString);
</code></pre>
<p>将一个十六进制转为一个十进制的数字.</p>
<h3 id="_35">参数</h3>
<ol>
<li><code>hexString``String</code> - 十六进制字符.</li>
</ol>
<h3 id="_36">返回值</h3>
<p><code>Number</code> - 传入字符串所代表的十六进制值.</p>
<h3 id="_37">示例</h3>
<pre><code class="js">var number = web3.toDecimal('0x15');
console.log(number); // 21
</code></pre>

<hr />
<h2 id="web3fromdecimal">web3.fromDecimal</h2>
<pre><code>web3.fromDecimal(number);
</code></pre>
<p>将一个数字，或者字符串形式的数字转为一个十六进制串.</p>
<h3 id="_38">参数</h3>
<ol>
<li><code>Number|String</code>  - 数字.</li>
</ol>
<h3 id="_39">返回值</h3>
<p><code>String</code> - 给定数字对应的十六进制表示.</p>
<h3 id="_40">示例</h3>
<pre><code class="js">var value = web3.fromDecimal('21');
console.log(value); // &quot;0x15&quot;
</code></pre>

<hr />
<h2 id="web3fromwei">web3.fromWei</h2>
<pre><code>web3.fromWei(number, unit)
</code></pre>
<p>以太坊货币单位之间的转换。将以wei为单位的资金，转换为指定单位的数值:</p>
<ul>
<li><code>Gwei</code></li>
<li><code>Kwei</code></li>
<li><code>Mwei</code>/<code>babbage</code>/<code>ether</code>/<code>femtoether</code></li>
<li><code>ether</code></li>
<li><code>finney</code>/<code>gether</code>/<code>grand</code>/<code>gwei</code></li>
<li><code>kether</code>/<code>kwei</code>/<code>lovelace</code>/<code>mether</code>/<code>micro</code></li>
<li><code>microether</code>/<code>milli</code>/<code>milliether</code></li>
<li><code>mwei</code>/<code>nano</code>/<code>nanoether</code></li>
<li><code>noether</code></li>
<li><code>picoether</code>/<code>shannon</code></li>
<li><code>szabo</code></li>
<li><code>tether</code></li>
<li><code>wei</code></li>
</ul>
<h3 id="_41">参数</h3>
<ol>
<li><code>number``Number|String|BigNumber</code> - 数字或BigNumber.</li>
<li><code>unit</code>String` - 单位字符串.</li>
</ol>
<h3 id="_42">返回值</h3>
<p><code>String|BigNumber</code> - Either a number string, or a BigNumber instance, depending on the given <code>number</code> parameter.</p>
<h3 id="_43">示例</h3>
<pre><code class="js">var value = web3.fromWei('21000000000000', 'finney');
console.log(value); // &quot;0.021&quot;
</code></pre>

<hr />
<h2 id="web3towei">web3.toWei</h2>
<pre><code>web3.toWei(number, unit)
</code></pre>
<p>将给定资金转换为以wei为单位的数值:</p>
<ul>
<li><code>kwei</code>/<code>ada</code></li>
<li><code>mwei</code>/<code>babbage</code></li>
<li><code>gwei</code>/<code>shannon</code></li>
<li><code>szabo</code></li>
<li><code>finney</code></li>
<li><code>ether</code></li>
<li><code>kether</code>/<code>grand</code>/<code>einstein</code></li>
<li><code>mether</code></li>
<li><code>gether</code></li>
<li><code>tether</code></li>
</ul>
<h3 id="_44">参数</h3>
<ol>
<li><code>Number|String|BigNumber</code> - 数字或BigNumber.</li>
<li><code>String</code> - 字符串单位.</li>
</ol>
<h3 id="_45">返回值</h3>
<p><code>String|BigNumber</code> - 根据传入参数的不同，分别是字符串形式的字符串，或者是BigNumber.</p>
<h3 id="_46">示例</h3>
<pre><code class="js">var value = web3.toWei('1', 'ether');
console.log(value); // &quot;1000000000000000000&quot;
</code></pre>

<hr />
<h2 id="web3tobignumber">web3.toBigNumber</h2>
<pre><code>web3.toBigNumber(numberOrHexString);
</code></pre>
<p>Converts a given number into a BigNumber instance.</p>
<p>See the <a href="#a-note-on-big-numbers-in-web3js">note on BigNumber</a>.</p>
<h3 id="_47">参数</h3>
<ol>
<li><code>Number|String</code> - 数字或十六进制格式的数字.</li>
</ol>
<h3 id="_48">返回值</h3>
<p><code>BigNumber</code> - BigNumber的实例.</p>
<h3 id="_49">示例</h3>
<pre><code class="js">var value = web3.toBigNumber('200000000000000000000001');
console.log(value); // instanceOf BigNumber
console.log(value.toNumber()); // 2.0000000000000002e+23
console.log(value.toString(10)); // '200000000000000000000001'
</code></pre>

<hr />
<h2 id="web3isaddress">web3.isAddress</h2>
<pre><code>web3.isAddress(HexString);
</code></pre>
<p>检查给定的字符串是否是有效的以太坊地址.</p>
<h3 id="_50">参数</h3>
<ol>
<li><code>String</code> - 16进制字符串.</li>
</ol>
<h3 id="_51">返回值</h3>
<p><code>Boolean</code> - 无效的地址返回false。对于全小写或全大写的有效地址字符串返回true。对于大小写混合的字符串, 该函数使用 <code>web3.isChecksumAddress()</code>进行检查.</p>
<h3 id="_52">示例</h3>
<pre><code class="js">var isAddress = web3.isAddress(&quot;0x8888f1f195afa192cfee860698584c030f4c9db1&quot;);
console.log(isAddress); // true
</code></pre></div>
        </div>

        <footer class="col-md-12">
            <hr>
            <p>Documentation built with <a href="https://www.mkdocs.org/">MkDocs</a>.</p>
        </footer>
        <script>
            var base_url = "..",
                shortcuts = {"help": 191, "next": 78, "previous": 80, "search": 83};
        </script>
        <script src="../js/base.js" defer></script>
        <script src="../search/main.js" defer></script>

        <div class="modal" id="mkdocs_search_modal" tabindex="-1" role="dialog" aria-labelledby="Search Modal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="exampleModalLabel">Search</h4>
            </div>
            <div class="modal-body">
                <p>
                    From here you can search these documents. Enter
                    your search terms below.
                </p>
                <form role="form">
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="Search..." id="mkdocs-search-query" title="Type search term here">
                    </div>
                </form>
                <div id="mkdocs-search-results"></div>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div><div class="modal" id="mkdocs_keyboard_modal" tabindex="-1" role="dialog" aria-labelledby="Keyboard Shortcuts Modal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="exampleModalLabel">Keyboard Shortcuts</h4>
            </div>
            <div class="modal-body">
              <table class="table">
                <thead>
                  <tr>
                    <th style="width: 20%;">Keys</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td class="help shortcut"><kbd>?</kbd></td>
                    <td>Open this help</td>
                  </tr>
                  <tr>
                    <td class="next shortcut"><kbd>n</kbd></td>
                    <td>Next page</td>
                  </tr>
                  <tr>
                    <td class="prev shortcut"><kbd>p</kbd></td>
                    <td>Previous page</td>
                  </tr>
                  <tr>
                    <td class="search shortcut"><kbd>s</kbd></td>
                    <td>Search</td>
                  </tr>
                </tbody>
              </table>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div>

    </body>
</html>
